/*
 * Created by JFormDesigner on Sat Nov 30 21:16:04 CST 2024
 */

package com.hafu.view.GYS;

import com.hafu.model.GysInfo;
import com.hafu.service.GysInfoService;
import com.hafu.util.StringUtil;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author 111
 */
public class AddGYSFrm extends JFrame {
    private GysInfoService gysInfoService=new GysInfoService();
    public AddGYSFrm() {
        initComponents();
    }

    private void button1(ActionEvent e) {
        String name = textField1.getText();
        if (StringUtil.isEmpty(name)) {
            JOptionPane.showMessageDialog(null, "供应商名称不能为空");
            return;
        }
        if (isNameExists(name)) {
            JOptionPane.showMessageDialog(null, "供应商名称已存在");
            return;
        }
        String tel = textField2.getText();
        if (StringUtil.isEmpty(tel)) {
            JOptionPane.showMessageDialog(null, "供应商电话不能为空");
            return;
        }
        GysInfo gysInfo = new GysInfo(name, tel);
        boolean added = gysInfoService.addGysInfo(gysInfo);
        if (added) {
            JOptionPane.showMessageDialog(null, "添加成功");
            fillTable();
        } else {
            JOptionPane.showMessageDialog(null, "添加失败");
        }
    }

    private void fillTable() {
    }

    // 新增方法：检查名称是否存在
    private boolean isNameExists(String name) {
        GysInfo gysInfo = new GysInfo();
        gysInfo.setName(name);
        ArrayList<GysInfo> gysInfoList = gysInfoService.getGysInfoByName(gysInfo);
        return !gysInfoList.isEmpty();
    }


    private void initComponents() {
        label1=new JLabel();
        textField1= new JTextField();


        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        textField1 = new JTextField();
        textField2 = new JTextField();
        button1 = new JButton();

        //======== this ========
        setTitle("\u6dfb\u52a0\u4f9b\u5e94\u5546");
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u4f9b\u5e94\u5546\u540d\u5b57");

        //---- label2 ----
        label2.setText("\u4f9b\u5e94\u5546\u7535\u8bdd");

        //---- button1 ----
        button1.setText("\u63d0\u4ea4");
        button1.addActionListener(e -> button1(e));

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(117, 117, 117)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                        .addComponent(label2, GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                        .addComponent(label1, GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE))
                    .addGap(70, 70, 70)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(button1)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 233, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 233, GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(148, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(141, 141, 141)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label1, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField1, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE))
                    .addGap(49, 49, 49)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
                        .addComponent(textField2, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE))
                    .addGap(66, 66, 66)
                    .addComponent(button1)
                    .addContainerGap(147, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JTextField textField1;
    private JTextField textField2;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
